<?php
/**
 * Created by PhpStorm.
 * User: user
 * Date: 15/5/17
 * Time: 下午3:03
 */

namespace push;

//require_cache('./getui/IGt.Push.php');
use jswei\push\sdk\geTui\igetui\NotifyInfo_Type;
use jswei\push\sdk\geTui\igetui\template\IGtTransmissionTemplate;
use jswei\push\sdk\geTui\igetui\template\notify\IGtNotify;
use think\facade\Log;

require_once(dirname(__FILE__) . '/getui/' . 'IGt.Push.php');
require_once(dirname(__FILE__) . '/getui/' . 'igetui/IGt.AppMessage.php');
require_once(dirname(__FILE__) . '/getui/' . 'igetui/IGt.APNPayload.php');
require_once(dirname(__FILE__) . '/getui/' . 'igetui/template/IGt.BaseTemplate.php');
require_once(dirname(__FILE__) . '/getui/' . 'IGt.Batch.php');
require_once(dirname(__FILE__) . '/getui/' . 'igetui/utils/AppConditions.php');

class GeTui {
    function __construct(){
        $gt_config = config('getui.');
        defined('APPKEY') or define('APPKEY',$gt_config['AppKey']);
        defined('APPID') or define('APPID',$gt_config['AppID']);
        defined('MASTERSECRET') or define('MASTERSECRET',$gt_config['MasterSecret']);
        defined('HOST') or define('HOST','http://sdk.open.api.igexin.com/apiex.htm');
    }

    /**
     * 模板---下载通知
     * @param $data 参数内容
     */
    function IGtNotyPopLoadTemplate($data){
        $template =  new \IGtNotyPopLoadTemplate();

        $template ->set_appId(APPID);//应用appid
        $template ->set_appkey(APPKEY);//应用appkey
        //通知栏
        $template ->set_notyTitle($data['tz_title']);//通知栏标题
        $template ->set_notyContent($data['tz_content']);//通知栏内容
        $template ->set_notyIcon("");//通知栏logo
        $template ->set_isBelled($data['is_belled']);//是否响铃
        $template ->set_isVibrationed($data['is_vibrationed']);//是否震动
        $template ->set_isCleared($data['is_cleared']);//通知栏是否可清除
        //弹框
        $template ->set_popTitle($data['tc_title']);//弹框标题
        $template ->set_popContent($data['tc_content']);//弹框内容
        $template ->set_popImage("");//弹框图片
        $template ->set_popButton1("下载");//左键
        $template ->set_popButton2("取消");//右键
        //下载
        $template ->set_loadIcon("");//弹框图片
        $template ->set_loadTitle($data['download_title']);
        $template ->set_loadUrl($data['download_url']);
        $template ->set_isAutoInstall(false);
        $template ->set_isActived(true);

        return $template;
    }

    //链接
    function IGtLinkTemplate($data){
        $template =  new \IGtLinkTemplate();
        $template ->set_appId(APPID);//应用appid
        $template ->set_appkey(APPKEY);//应用appkey
        $template ->set_title($data['tz_title']);//通知栏标题
        $template ->set_text($data['tz_content']);//通知栏内容
        $template ->set_logo("");//通知栏logo
        $template ->set_isRing($data['is_belled']);//是否响铃
        $template ->set_isVibrate($data['is_vibrationed']);//是否震动
        $template ->set_isClearable($data['is_cleared']);//通知栏是否可清除
        $template ->set_url($data['link_url']);//打开连接地址
        return $template;
    }

    //通知透传模板
    function IGtNotificationTemplate($data){
        $template =  new \IGtNotificationTemplate();
        $template->set_appId(APPID);//应用appid
        $template->set_appkey(APPKEY);//应用appkey
        $template->set_transmissionType(1);//透传消息类型
        $template->set_transmissionContent($data['touchuan_content']);//透传内容
        $template->set_title($data['tz_title']);//通知栏标题
        $template->set_text($data['tz_content']);//通知栏内容
        $template->set_logo('');//通知栏logo
        $template->set_isRing($data['is_belled']);//是否响铃
        $template->set_isVibrate($data['is_vibrationed']);//是否震动
        $template->set_isClearable($data['is_cleared']);//通知栏是否可清除
        return $template;
    }

    //透传模板
    function IGtTransmissionTemplate($data){
        $template =  new \IGtTransmissionTemplate();
        $template->set_appId(APPID);//应用appid
        $template->set_appkey(APPKEY);//应用appkey
        $template->set_transmissionType(1);//透传消息类型
        $template->set_transmissionContent($data['touchuan_content']);//透传内容
        return $template;
    }

    function IGtTransmissionTemplate1($data){
        $package = $data['package']??'com.jiexunet';
        $intent = "intent:#Intent;launchFlags=0x10000000;package=com.pp.yl;component=".$package."/com.getui.demo.MainActivity;i.parm1=12;end";
        $template = new IGtTransmissionTemplate();
        $template->set_appId(APPID);//应用appid
        $template->set_appkey(APPKEY);//应用appkey
        $template->set_transmissionType(2);//透传消息类型
        $template->set_transmissionContent(json_encode($data['touchuan_content']));//透传内容
        $notify = new IGtNotify();
        $notify -> set_title($data['title']);
        $notify -> set_content($data['content']);
        $notify -> set_intent($intent);
        $notify -> set_type(NotifyInfo_Type::_intent);
        $template->set3rdNotifyInfo($notify);
        return $template;
    }


    //IOS透传模板
    function IgtTransmissionTemplateIOS($data){
        Log::write('ios:'.json_encode($data));
        $template =  new \IGtTransmissionTemplate();
        $template->set_appId(APPID);//应用appid
        $template->set_appkey(APPKEY);//应用appkey
        $template->set_transmissionType(1);//透传消息类型
        $template->set_transmissionContent($data['touchuan_content']);//透传内容
        //$template->set_duration(BEGINTIME,ENDTIME); //设置ANDROID客户端在此时间区间内展示消息

//       APN高级推送
        $apn = new \IGtAPNPayload();
        $alertmsg=new \DictionaryAlertMsg();
        $alertmsg->body=$data['tz_content'];
        $alertmsg->actionLocKey="ActionLockey";
//        $alertmsg->locKey="LocKey";
        $alertmsg->locArgs=array("locargs");
        $alertmsg->launchImage="launchimage";
//        IOS8.2 支持
        $alertmsg->title=$data['tz_title'];
//        $alertmsg->titleLocKey="TitleLocKey";
        $alertmsg->titleLocArgs=array("TitleLocArg");

        $apn->alertMsg=$alertmsg;
        $apn->badge=1;
        $apn->sound="";
        $apn->add_customMsg("title",$data['tz_title']);
        $apn->add_customMsg("touchuan_content",$data['touchuan_content']);
//      $apn->contentAvailable=1;
        $apn->category="ACTIONABLE";
        $template->set_apnInfo($apn);

        return $template;
    }

    //单个用户推送
    public function SendOneUser($data,$cid,$dtoken,$type=1,&$out){
        //
        $igt = new \IGeTui(HOST,APPKEY,MASTERSECRET);
        $userStatus = $igt->getClientIdStatus(APPID,$cid);
        $template = false;
        if(!empty($dtoken)){
            if($userStatus['result'] == 'Offline'){
                $template = $this->IgtTransmissionTemplateIOS($data);
            }else{
                $template = $this->IGtTransmissionTemplate($data);
            }
        }
        if(!$template){
            switch ($type) {
                case 1:
                    $template = $this->IGtNotyPopLoadTemplate($data);
                    break;
                case 2:
                    $template = $this->IGtLinkTemplate($data);
                    break;
                case 3:
                    $template = $this->IGtNotificationTemplate($data);
                    break;
                case 4:
                    $template = $this->IGtTransmissionTemplate($data);
                    break;
                default:
                    break;
            }
        }


        //个推信息体
        $message = new \IGtSingleMessage();

        $message->set_isOffline(true);//是否离线
        $message->set_offlineExpireTime(3600*12*1000);//离线时间
        $message->set_data($template);//设置推送消息类型
        $message->set_PushNetWorkType(0);//设置是否根据WIFI推送消息，1为wifi推送，0为不限制推送
        //接收方
        $target = new \IGtTarget();
        $target->set_appId(APPID);
        $target->set_clientId($cid);
        if(!empty($dtoken)){
            if($userStatus['result'] == 'Offline'){
                Log::write('dtoken3:'.$dtoken);
                $out = $igt->pushAPNMessageToSingle(APPID,$dtoken,$message);
            }else{
                $out = $igt->pushMessageToSingle($message,$target);
            }
        }else{
            $out = $igt->pushMessageToSingle($message,$target);
        }
        if($out['result'] == 'ok'){
            return true;
        }
        return false;
    }

    /**
     * uni-app 推送
     * @param $payload
     * @param $cidArr
     * @param string $begin
     * @param string $end
     * @param null $out
     * @param string $package
     * @return boolean
     * @throws \Exception
     */
    function pushMessageToUniAppList($payload,$cidArr,$begin='',$end='',&$out=null,$package='com.jiexunet'){
        if(empty($cidArr)){
            $out = '没有可以发送的对象';
            return false;
        }
        $intent = 'intent:#Intent;action=android.intent.action.oppopush;launchFlags=0x14000000;component='.$package.'/io.dcloud.UNI70C8F6C;S.UP-OL-SU=true;S.title='.$payload['title'].';S.content='.$payload['content'].';S.payload='.json_encode($payload).';end';
        $template = new IGtTransmissionTemplate();
        $template->set_appId(APPID);
        $template->set_appkey(APPKEY);
        $template->set_transmissionType(2);
        $template->set_transmissionContent(json_encode($payload));
        if($begin && $end){
            $template->set_duration($begin,$end);
        }
        $notify = new IGtNotify();
        $notify -> set_title($payload['title']);
        $notify -> set_content($payload['content']);
        $notify -> set_intent($intent);
        $notify -> set_type(NotifyInfo_Type::_intent);
//        $notify -> set_url("https://dev.getui.com/");
        $template ->set3rdNotifyInfo($notify);

        $igt = new \IGeTui(HOST,APPKEY,MASTERSECRET);
        //个推信息体
        $message = new \IGtListMessage();
        $message->set_data($template);//设置推送消息类型
        $message->set_isOffline(true);//是否离线
        $message->set_offlineExpireTime(3600*12*1000);//离线时间
        $message->set_PushNetWorkType(0);//设置是否根据WIFI推送消息，1为wifi推送，0为不限制推送
        $contentId = $igt->getContentId($message);
        $targetList = array();
        if(!empty($cidArr)){
            foreach($cidArr as $key => $value){
                $target = new \IGtTarget();
                $target->set_appId(APPID);
                $target->set_clientId($value);
                $targetList[] = $target;
            }
        }
        $out =  $igt->pushMessageToList($contentId,$targetList);
        if ($out['result'] != 'ok'){
            return false;
        }
        return true;
    }



    //多推接口案例
    function pushMessageToList($data,$cidArr,$dtokenArr,$type=1,&$out=null,$begin='',$end='',$tpl=null){
        if(empty($cidArr) && empty($dtokenArr)){
            $out = '没有可以发送的对象';
            return false;
        }

        switch ($type) {
            case 1:
                $template = $this->IGtNotyPopLoadTemplate($data);
                break;
            case 2:
                $template = $this->IGtLinkTemplate($data);
                break;
            case 3:
                $template = $this->IGtNotificationTemplate($data);
                break;
            case 4:
                $template = $this->IGtTransmissionTemplate($data);
                break;
            case 5:
                $template = $tpl;
                break;
            default:
                $template = $this->IGtNotyPopLoadTemplate($data);
                break;
        }
        if(!empty($dtokenArr)){
            if($type!=4){
                $template ->set_pushInfo("",1,$data['tz_content'], "", $data['touchuan_content'], "", "", "");
            }else{
                $message = !empty($data['message']) ? $data['message'] : (!empty($data['tz_content']) ? $data['tz_content'] : '您收到一条消息');
                $template ->set_pushInfo("",1,$message, "", "", "", "", "");
                unset($message);
            }
        }
        if($begin && $end){
            $template->set_duration($begin,$end);
        }
        $igt = new \IGeTui(HOST,APPKEY,MASTERSECRET);
        //个推信息体
        $message = new \IGtListMessage();
        $message->set_data($template);//设置推送消息类型
        $contentId1 = $igt->getAPNContentId(APPID,$message);
        $message->set_isOffline(true);//是否离线
        $message->set_offlineExpireTime(3600*12*1000);//离线时间
        $message->set_PushNetWorkType(0);//设置是否根据WIFI推送消息，1为wifi推送，0为不限制推送
        $contentId = $igt->getContentId($message);
        $out1 = array();
        if(!empty($dtokenArr)){
            $dtokenArr = array_values($dtokenArr);
            $out1 = $igt->pushAPNMessageToList(APPID,$contentId1,$dtokenArr);
        }
        $targetList = array();
        if(!empty($cidArr)){
            foreach($cidArr as $key => $value){
                $target = new \IGtTarget();
                $target->set_appId(APPID);
                $target->set_clientId($value);
                $targetList[] = $target;
            }
            $out2 = $igt->pushMessageToList($contentId,$targetList);
        }
        $data['is_send'] = 0;
        if(!empty($out1)){
            if($out1['result'] == 'ok') {
                $data['is_send'] = 1;
                $out[] = 'IOS用户发送成功';
                $out['result'] = "ok";
            }else{
                $out[] = 'IOS用户发送异常';
            }
        }else{
            $out1['result'] = 'ok';
        }

        if(!empty($out2)){
            if($out2['result'] == 'ok'){
                $data['is_send'] = 1;
                $out[] = '安卓用户发送成功';
                $out['result'] = "ok";
            }else{
                $out[] = '安卓用户发送异常';
            }
        }else{
            $out2['result']='ok';
        }
        if($out1['result'] == 'ok' && $out2['result'] == 'ok'){
            return true;
        }else{
            return false;
        }
//        $data['remark'] = !empty($out) ? implode(',', $out) : '';
//        $info['send_time'] = time();
//        if(empty($data['id'])){
//            $data['add_time'] = time();
//            $data['is_now_send'] = 1;
//            $info['contentId']= $out['contentId'];
//            if($this -> addTuiData(array_merge($data,$info))){
//                return true;
//            }
//        }else{
//            $where['id'] = $data['id'];
//            if($GetuiModel ->where($where)-> save(array_merge($info,$data))){
//                return true;
//            }
//        }
//        return false;
    }


    function pushMessageToListV3($data,$cidArr,$is_ios,$type=1,&$out='')
    {
        //ios系统仅支持type = 4 。android支持其它
        if (empty($cidArr)) {
            $out = '没有可以发送的对象';
            return false;
        }
        if($is_ios == 0){
            if($type == 4){
                $type = 3;
            }
        }else{
            $type = $type == 4 ? 4 : 5;
        }
        switch ($type) {
            case 1:
                $template = $this->IGtNotyPopLoadTemplate($data);
                break;
            case 2:
                $template = $this->IGtLinkTemplate($data);
                break;
            case 3:
                $template = $this->IGtNotificationTemplate($data);
                break;
            case 4:
                $template = $this->IgtTransmissionTemplateIOS($data);
                break;
            case 5:
                $template = $this->IGtTransmissionTemplate($data);
                break;
            default:
                $out = '类型错误';
                return false;
                break;
        }

        if ($is_ios == 1) {
            $message = !empty($data['message']) ? $data['message'] : (!empty($data['tz_content']) ? $data['tz_content'] : '您收到一条消息');
            $template ->set_pushInfo("",1,$message, "", "", "", "", "");
            unset($message);
        }else{
            $template->set_pushInfo("", 1, $data['tz_content'], "", $data['touchuan_content'], "", "", "");

        }
        $igt = new \IGeTui(HOST, APPKEY, MASTERSECRET);
        //个推信息体
        $message = new \IGtListMessage();
        $message->set_data($template);//设置推送消息类型
        $message->set_isOffline(true);//是否离线

        $message->set_offlineExpireTime(86400);//离线时间
        $message->set_PushNetWorkType(0);//设置是否根据WIFI推送消息，1为wifi推送，0为不限制推送

        $out = array();
        $targetList = array();
        if (!empty($cidArr)) {
            $contentId = $igt->getContentId($message);
            foreach ($cidArr as $key => $value) {
                $target = new \IGtTarget();
                $target->set_appId(APPID);
                $target->set_clientId($value);
                $targetList[] = $target;
            }
            $out = $igt->pushMessageToList($contentId, $targetList);
        }

        Log::write('send_r:'.json_encode($out));
        if (!empty($out)) {
            if ($out['result'] == 'ok') {
                return true;
            }
        }
        $out = '发送异常';
        return false;

    }

    function get_template($data,$type){
        $template = '';
        switch ($type) {
            case 1:
                $template = $this->IGtNotyPopLoadTemplate($data);
                break;
            case 2:
                $template = $this->IGtLinkTemplate($data);
                break;
            case 3:
                $template = $this->IGtNotificationTemplate($data);
                break;
            case 4:
                $template = $this->IgtTransmissionTemplateIOS($data);
                break;
            case 5:
                $template = $this->IGtTransmissionTemplate($data);
                break;
            default:
                break;
        }
        return $template;
    }

    function pushMessageToListV4($data,$cidArr,$is_ios,&$out)
    {
        //ios系统仅支持type = 4 。android支持其它
        if (empty($cidArr)) {
            $out = '没有可以发送的对象';
            return false;
        }
        $igt = new \IGeTui(HOST, APPKEY, MASTERSECRET);
        $online_cidarr = $offline_cidarr = array();
        foreach($cidArr as $v){
            $cid = $v['cid'];
            if($is_ios==1){
                $userStatus = $igt->getClientIdStatus(APPID,$cid);
                if($userStatus['result'] == 'Offline'){
                    $offline_cidarr[] = $v;
                }else{
                    $online_cidarr[] = $v;
                }
            }else{
                $online_cidarr[] = $v;
            }
        }

        if($is_ios == 1){
            //发送在线
            if(!empty($online_cidarr)){
                $online_template = self::get_template($data,5);
                $message = !empty($data['message']) ? $data['message'] : (!empty($data['tz_content']) ? $data['tz_content'] : '您收到一条消息');
                $online_template ->set_pushInfo("",1,$message, "", "", "", "", "");
                $message = new \IGtListMessage();
                $message->set_data($online_template);//设置推送消息类型
                $message->set_isOffline(true);//是否离线
                $message->set_offlineExpireTime(86400);//离线时间
                $message->set_PushNetWorkType(0);//设置是否根据WIFI推送消息，1为wifi推送，0为不限制推送
                $contentId = $igt->getContentId($message);
                $targetList = array();
                foreach($online_cidarr as $v){
                    $target = new \IGtTarget();
                    $target->set_appId(APPID);
                    $target->set_clientId($v['cid']);
                    $targetList[] = $target;
                }
                $out = $igt->pushMessageToList($contentId, $targetList);
                Log::write('send_r1:'.json_encode($out));
            }
            if(!empty($offline_cidarr)){
                $offline_template = self::get_template($data,4);
                $message = !empty($data['message']) ? $data['message'] : (!empty($data['tz_content']) ? $data['tz_content'] : '您收到一条消息');
                $offline_template ->set_pushInfo("",1,$message, "", "", "", "", "");
                $message = new \IGtListMessage();
                $message->set_data($offline_template);//设置推送消息类型
                $message->set_isOffline(true);//是否离线
                $message->set_offlineExpireTime(86400);//离线时间
                $message->set_PushNetWorkType(0);//设置是否根据WIFI推送消息，1为wifi推送，0为不限制推送
                $contentId = $igt->getAPNContentId(APPID,$message);
                $targetList = array();
                foreach($offline_cidarr as $v){
                    $targetList[] = $v['dt'];
                }
                $out = $out = $igt->pushAPNMessageToList(APPID,$contentId,$targetList);
                Log::write('send_r2:'.json_encode($out));
            }

        }else{
            if(!empty($online_cidarr)){
                $template = self::get_template($data,4);
                $template->set_pushInfo("", 1, $data['tz_content'], "", $data['touchuan_content'], "", "", "");
                $message = new \IGtListMessage();
                $message->set_data($template);//设置推送消息类型
                $message->set_isOffline(true);//是否离线
                $message->set_offlineExpireTime(86400);//离线时间
                $message->set_PushNetWorkType(0);//设置是否根据WIFI推送消息，1为wifi推送，0为不限制推送
                $contentId = $igt->getContentId($message);
                $targetList = array();
                foreach($online_cidarr as $v){
                    $target = new \IGtTarget();
                    $target->set_appId(APPID);
                    $target->set_clientId($v['cid']);
                    $targetList[] = $target;
                }
                $out = $igt->pushMessageToList($contentId, $targetList);
                Log::write('send_r3:'.json_encode($out));
            }

        }
        return true;
    }


    function pushMessageToListV2($data,$cidArr,$dtokenArr,$type=1,&$out)
    {
        //ios系统仅支持type = 4 。android支持其它
        if (empty($cidArr) && empty($dtokenArr)) {
            $out = '没有可以发送的对象';
            return false;
        }
        if(empty($dtokenArr)){
            if($type == 4 || $type == 5){
                $type = 3;
            }
        }else{
            $type = $type == 4 ? 4 : 5;
        }
        switch ($type) {
            case 1:
                $template = $this->IGtNotyPopLoadTemplate($data);
                break;
            case 2:
                $template = $this->IGtLinkTemplate($data);
                break;
            case 3:
                $template = $this->IGtNotificationTemplate($data);
                break;
            case 4:
                $template = $this->IgtTransmissionTemplateIOS($data);
                break;
            case 5:
                $template = $this->IGtTransmissionTemplate($data);
                break;
            default:
                $out = '类型错误';
                return false;
                break;
        }

        if (!empty($dtokenArr)) {
            $cidArr = array();
            $message = !empty($data['message']) ? $data['message'] : (!empty($data['tz_content']) ? $data['tz_content'] : '您收到一条消息');
            $template ->set_pushInfo("",1,$message, "", "", "", "", "");
            unset($message);
        }else{
            $template->set_pushInfo("", 1, $data['tz_content'], "", $data['touchuan_content'], "", "", "");

        }
        $igt = new \IGeTui(HOST, APPKEY, MASTERSECRET);
        //个推信息体
        $message = new \IGtListMessage();
        $message->set_data($template);//设置推送消息类型
        $message->set_isOffline(true);//是否离线

        $message->set_offlineExpireTime(86400);//离线时间
        $message->set_PushNetWorkType(0);//设置是否根据WIFI推送消息，1为wifi推送，0为不限制推送

        $out = array();
        if (!empty($dtokenArr)) {
            $contentId1 = $igt->getAPNContentId(APPID, $message);
            $dtokenArr = array_values($dtokenArr);
            $out = $igt->pushAPNMessageToList(APPID, $contentId1, $dtokenArr);
        }else{
            $targetList = array();
            if (!empty($cidArr)) {
                $contentId = $igt->getContentId($message);
                foreach ($cidArr as $key => $value) {
                    $target = new \IGtTarget();
                    $target->set_appId(APPID);
                    $target->set_clientId($value);
                    $targetList[] = $target;
                }
                $out = $igt->pushMessageToList($contentId, $targetList);
            }
        }
        Log::write('send_r:'.json_encode($out));
        if (!empty($out)) {
            if ($out['result'] == 'ok') {
                return true;
            }
        }
        $out = '发送异常';
        return false;

    }


    function pushMessageToAppIos($data,$params=array(),&$out){
        $igt = new \IGeTui(HOST,APPKEY,MASTERSECRET);
        $template = $this->IgtTransmissionTemplateIOS($data);

        $message = new \IGtAppMessage();
        $message->set_isOffline(true);
        $message->set_offlineExpireTime(3600*12*1000);//离线时间单位为毫秒，例，两个小时离线为3600*1000*2
        $message->set_data($template);
        $message->set_PushNetWorkType(0);	//设置是否根据WIFI推送消息，1为wifi推送，0为不限制推送
        $message->set_appIdList(array(APPID));
        $cdt = new \AppConditions();
        $cdt -> addCondition2(\AppConditions::PHONE_TYPE,array('IOS'));

        $message->set_phoneTypeList(array('IOS'));

    }

    function pushMessageToAppAndroid($data,$param=array(),$type,&$out){

    }


    //群推接口案例
    function pushMessageToApp($data=[],$params=array(),$type=3,&$out=null,$phoneTypeList='',$provinceList='',$tagList=''){
        $igt = new \IGeTui(HOST,APPKEY,MASTERSECRET);
        switch ($type) {
            case 1:
                $template = $this->IGtNotyPopLoadTemplate($data);
                break;
            case 2:
                $template = $this->IGtLinkTemplate($data);
                break;
            case 3:
                $template = $this->IGtNotificationTemplate($data);
                break;
            case 4:
                $template = $this->IgtTransmissionTemplateIOS($data);
                break;
            default:
                break;
        }

        if($type!=4){
            $template ->set_pushInfo("",1,$data['tz_content'], "", $data['touchuan_content'], "", "", "");
        }else{
            $message = !empty($data['message']) ? $data['message'] : (!empty($data['tz_content']) ? $data['tz_content'] : '您收到一条消息');
            $template ->set_pushInfo("",1,$message, "", "", "", "", "");
            unset($message);
        }
        //个推信息体
        //基于应用消息体
        $message = new \IGtAppMessage();

        $message->set_isOffline(true);
        $message->set_offlineExpireTime(3600*12*1000);//离线时间单位为毫秒，例，两个小时离线为3600*1000*2
        $message->set_data($template);
        $message->set_PushNetWorkType(0);	//设置是否根据WIFI推送消息，1为wifi推送，0为不限制推送
        $message->set_appIdList(array(APPID));
        $cdt = new \AppConditions();
        if($phoneTypeList){
            $cdt->addCondition(\AppConditions::PHONE_TYPE, $phoneTypeList);
        }
        if($provinceList){
            $cdt->addCondition(\AppConditions::REGION, $provinceList);
            $message->set_provinceList($provinceList);
        }
        if($tagList){
            $cdt->addCondition(\AppConditions::TAG, $tagList,1);
        }
//        $message->set_conditions($cdt->getCondition());

//        p($message);die;
        if($type == 4){
            $message->set_phoneTypeList(array('IOS'));
        }else{
            $message->set_phoneTypeList(array('ANDROID'));
        }
//        if(!empty($params['phoneTypeList'])){
//            $message->set_phoneTypeList($params['phoneType']);
//        }
//        if(!empty($params['provinceList'])){
//            $message->set_provinceList($params['provinceList']);
//        }
//        if(!empty($params['tagList'])){
//            $message->set_tagList($params['tagList']);
//        }
        $appName = !empty($params['appName']) ? $data['appName'] : '';
//        p($message);die;
        $out = $igt->pushMessageToApp($message,$appName); //根据TaskId设置组名，支持下划线，中文，英文，数字

//        $where['id'] = $data['id'];
////        $GetuiModel = new GetuiModel();
//        $info['send_time'] = time();
//        $info['is_send'] = 1;
        if($out['result'] == 'ok'){
            return true;
//            if(empty($data['id'])){
//                $data['add_time'] = time();
//                $data['is_now_send'] = 1;
//                $info['contentId']= $out['contentId'];
//                $data['sender_type'] = 1;
//                if($this -> addTuiData(array_merge($data,$info))){
//                    return true;
//                }
//            }else{
//                if($GetuiModel ->where($where)-> save($info)){
//                    return true;
//                }
//            }
        }
        return false;
    }

//    function getGetuiList($where,$status, $page, $pagesize){
//        $GetuiModel = new GetuiModel();
//        $list = $GetuiModel -> GetList($where,true,'id desc',$page,$pagesize);
//        return $list;
//    }

//    function getGetuiDetail($id){
//        $GetuiModel = new GetuiModel();
//        $list = $GetuiModel -> GetInfo($id);
//        if(!empty($list['uid'])){
//            $UserModel = new UserModel();
//            $u_list = $UserModel->GetInfo($list['uid'],'user_nick');
//            if(!empty($u_list)){
//                $list['user_nick'] = $u_list['user_nick'];
//            }
//        }
//        return $list;
//    }

    /**
     *添加个推数据(原geTuiAll方法)
     */
//    function addTuiData($data){
//        $data['add_time'] = time();
//        $GetuiModel = new GetuiModel();
//        $field = array('type','tz_title','tz_content','tc_title','tc_content','tc_img','download_title','download_url','is_belled','is_vibrationed','is_cleared','link_url','touchuan_content','client_id','uid','sender_type','last_login','last_login','mobiles','is_now_send','send_time','is_send','contentId','stop_time','add_time','send_type','remark','client_status');
//        foreach($data as $k => $v){
//            if(!in_array($k,$field)){
//                unset($data[$k]);
//            }
//        }
//        $res = $GetuiModel -> add($data);
//        if($res){
//            return $res;
//        }else{
//            return false;
//        }
//    }
}